package com.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wx.entity.SmbmsBill;
import com.wx.entity.SmbmsProvider;
import com.wx.mapper.SmbmsBillMapper;
import com.wx.mapper.SmbmsProviderMapper;
import com.wx.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @author wangxiao
 * @since 2023-03-17 16:50:41
 */
//@Slf4j
@Service
//@RequiredArgsConstructor
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SmbmsProviderServiceImpl implements SmbmsProviderService {

    @Autowired
    private SmbmsProviderMapper smbmsProviderMapper;
    @Autowired
    SmbmsBillMapper smbmsBillMapper;

    @Override
    public void addSmbmsProvider(SmbmsProvider smbmsProvider) {
        smbmsProviderMapper.insert(smbmsProvider);
    }

    //假删除  isDelete  1 为删除 2 已删除
    //淘宝订单信息。删除
    //
    @Override
    public void delSmbmsProvider(String id) {
        //1. 判断 订单表中，要删除的供应商ID ，如果存在，不让删除。
        //2 都删除
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("provider_id",id);
       List<SmbmsBill> list =  smbmsBillMapper.selectList(queryWrapper);
       if(list.size()<=0){
           smbmsProviderMapper.deleteById(id);
       }
    }

    @Override
    public void updSmbmsProvider(SmbmsProvider smbmsProvider) {
        smbmsProviderMapper.updateById(smbmsProvider);
    }

    @Override
    public SmbmsProvider getSmbmsProviderById(String id) {
        return smbmsProviderMapper.selectById(id);
    }

    @Override
    public IPage<SmbmsProvider> getSmbmsProviderByPage(Page page, SmbmsProvider smbmsProvider) {
        LambdaQueryWrapper<SmbmsProvider> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (smbmsProvider != null) {
            if (StringUtils.isNotEmpty(smbmsProvider.getProCode())) {
                lambdaQueryWrapper.eq(SmbmsProvider::getProCode, smbmsProvider.getProCode());
            }
        }
        IPage<SmbmsProvider> smbmsProviderIPage = smbmsProviderMapper.selectPage(page, lambdaQueryWrapper);
        return smbmsProviderIPage;
    }


}